package com.edu.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.CommonResult;
import com.edu.entity.Conditions;
import com.edu.entity.Goods;
import com.edu.entity.GoodsToWeb;
import com.edu.entity.Orders;
import com.edu.entity.OrdersDTO;
import com.edu.entity.Page;
import com.edu.service.GoodService;
import com.edu.service.OrdersService;
import com.edu.service.impl.GoodServiceImpl;
import com.edu.service.impl.OrdersServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/selectAllOrders")
public class findOrdersServlet extends HttpServlet {
	OrdersService os = new OrdersServiceImpl();
	CommonResult cr = new CommonResult();
	GoodService gs = new GoodServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ObjectMapper om = new ObjectMapper();
		OrdersDTO orderDto = new OrdersDTO();
		Conditions con = new Conditions();
		Goods goods = new Goods();
		ArrayList<GoodsToWeb> listGoods = new ArrayList<GoodsToWeb>();
		ArrayList listgood = new ArrayList();
		String target_user_id_String = request.getParameter("target_user_id");
		String pageNumString = request.getParameter("pageNum");
		String pageSizeString = request.getParameter("pageSize");
		int rowNum = 0;
		int pageSize = 0;
		int pageNum = 0;
		int goodsid = 0;
		System.out.println(target_user_id_String+"----123");
		try {
			int target_user_id = Integer.parseInt(target_user_id_String);
			List<Orders> listOrders = os.getAllOrders(target_user_id);
			orderDto.setListOrders(listOrders);
			System.out.println(listOrders);
			for (int i = 0; i < listOrders.size(); i++) {
				goodsid = listOrders.get(i).getCommodityId();
				System.out.println(goodsid);
				goods.setGoodid(goodsid);
				listGoods = gs.indexFindGoods(goods, null);
				listgood.add(listGoods);
			}
			
			orderDto.setListGoods(listgood);
			rowNum = os.getAllOrdersNums(target_user_id);
			cr.setCode(200);
			cr.setMessage("success");
		} catch (Exception e) {
			int target_user_id = 0 ;
			cr.setCode(404);
			cr.setMessage("file");
		}
		try {
			pageSize = Integer.parseInt(pageSizeString);
		} catch (Exception e) {
			pageSize = 10;
		}
		try {
			pageNum = Integer.parseInt(pageNumString);
		} catch (Exception e) {
			pageNum = 1;
		}
		List<OrdersDTO> pageList = new ArrayList<OrdersDTO>();
		pageList.add(orderDto);
		Page page = new Page(pageList,pageNum,pageSize,rowNum);
		cr.setData(page);
//		System.out.println(page);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(cr.toJsonString());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
